package com.zwk.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwk.base.entity.CommonResult;
import com.zwk.edu.entity.Course;
import com.zwk.edu.entity.Teacher;
import com.zwk.edu.entity.vo.CourseInfoVo;
import com.zwk.edu.entity.vo.CoursePublishVo;
import com.zwk.edu.entity.vo.CourseQuery;
import com.zwk.edu.entity.vo.TeacherQuery;
import com.zwk.edu.service.CourseService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zwk
 * @since 2022-08-22
 */
@RestController
@RequestMapping("/edu/course")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    // 添加课程基本信息
    @PostMapping("/addCourseInfo")
    public CommonResult addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        String id = courseService.saveCourseInfo(courseInfoVo);

        return CommonResult.ok().data("courseId", id);
    }

    // 根据课程id查询课程信息
    @GetMapping("/getCourseInfo/{courseId}")
    public CommonResult getCourseInfo(@PathVariable("courseId") String courseId){

        CourseInfoVo courseInfoVo = courseService.getCourseById(courseId);
        return CommonResult.ok().data("courseInfoVo", courseInfoVo);
    }

    // 修改课程信息
    @PostMapping("/updateCourseInfo")
    public CommonResult updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        courseService.updateCourseInfo(courseInfoVo);

        return CommonResult.ok();
    }

    // 根据课程id查询所有课程相关信息
    @GetMapping("/getPublishCourseInfo/{id}")
    public CommonResult getPublishCourseInfo(@PathVariable("id") String id){
        CoursePublishVo publishVo = courseService.publishCourseInfo(id);

        return CommonResult.ok().data("publishCourse", publishVo);
    }

     // 课程发表, 修改课程status
    @PostMapping("/publishCourse/{id}")
    public CommonResult publishCourse(@PathVariable("id") String id){

        Course course = new Course();
        course.setId(id);
        course.setStatus("Normal");

        boolean result = courseService.updateById(course);
        if (result){
            return CommonResult.ok();
        }
        return CommonResult.error();
    }

    // 分页查询课程信息
    @GetMapping("/page/{current}")
    public CommonResult getPageList(@PathVariable("current") Integer current){

        //创建page对象
        Page<Course> pageCourse = new Page<>(current,5);
        IPage<Course> page = courseService.page(pageCourse, null);

        // 判断current是否合法
        if(current > page.getPages()){
            current = Math.toIntExact(page.getPages());
            pageCourse = new Page<>(current,5);
            page = courseService.page(pageCourse, null);
        }

        long total = page.getTotal();
        List<Course> records = page.getRecords();

        return CommonResult.ok().data("total", total).data("records", records);

    }

    // 查询所有课程信息
    @GetMapping("/getCourseList")
    public CommonResult getCourseList(){

        List<Course> list = courseService.list(null);

        return CommonResult.ok().data("list", list);

    }

    // 条件查询
    @ApiOperation(value = "条件查询")
    @PostMapping("/pageCourseCondition/{current}")
    public CommonResult pageTeacherCondition(@PathVariable("current") Integer current,
                                             @RequestBody(required = false) CourseQuery courseQuery){


        Page<Course> pageCourse = new Page<>(current,5);

        // 条件构造器
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(courseQuery.getTitle())){
            queryWrapper.like("title", courseQuery.getTitle());
        }
        if (!ObjectUtils.isEmpty(courseQuery.getStatus())){
            queryWrapper.eq("status", courseQuery.getStatus());
        }

        // 排序
        queryWrapper.orderByDesc("gmt_create");

        IPage<Course> page = courseService.page(pageCourse, queryWrapper);

        // 判断current是否合法
        if(current > page.getPages()){
            current = Math.toIntExact(page.getPages());
            pageCourse = new Page<>(current,5);
            page = courseService.page(pageCourse, queryWrapper);
        }

        long total = page.getTotal();
        List<Course> records = page.getRecords();

        return CommonResult.ok().data("total", total).data("records", records);

    }

    // 删除课程
    @DeleteMapping("/{courseId}")
    public CommonResult deleteCourseById(@PathVariable("courseId") String courseId){

        courseService.removeCourse(courseId);

        return CommonResult.ok();
    }

}

